﻿using System;
using System.Web.Services.Protocols;
using System.Globalization;

namespace CSP.CRM.ISVExtensions
{



  public partial class ResolveCasePage : System.Web.UI.Page
  {
    private CspInfo csp;
    private Guid caseId;
    private string action;
    private int languageCode;

    protected void Page_Load(object sender, EventArgs e)
    {
      try
      {
        Initialize();
        if (action == "resolve")
        {
          string subject = csp.GetConfigText("CaseResolutionSubject");
          string description = csp.GetConfigText("CaseResolutionDescription");
          csp.ResolveCase(caseId, subject, description);
        }
        else if (action == "cancel")
          csp.CancelCase(caseId);
        string closeScript = "window.opener.close();window.close();";
        ClientScript.RegisterClientScriptBlock(typeof(string), "kScript", closeScript, true);
      }
      catch (SoapException)
      {
        lbMessage.Text = csp.GetConfigText("ResolveCaseError");
      }
    }

    protected void Initialize()
    {
      string organizationName = null;

      if (Request["id"] != null)
        caseId = new Guid(Request["id"]);
      if (Request["action"] != null)
        action = Request["action"];
      if (Request["orgname"] != null)
        organizationName = Request["orgname"];
      if (Request["userlcid"] != null)
        languageCode = Int32.Parse(Request["userlcid"], CultureInfo.InvariantCulture);

      System.Net.IPHostEntry ipEntries = System.Net.Dns.GetHostEntry(Request.Url.Host);
      string strServerIp = ipEntries.AddressList[0].ToString();
      string crmServerUrl = String.Format(CultureInfo.InvariantCulture, "{0}://{1}:{2}", Request.Url.Scheme, strServerIp, Request.Url.Port);

//      string crmServerUrl = String.Format(CultureInfo.InvariantCulture, "{0}://{1}:{2}", Request.Url.Scheme, Request.Url.Host, Request.Url.Port);
      csp = new CspInfo(new Uri(crmServerUrl), organizationName, languageCode);

    }

  }
}
